package com.shj.em.aa.controller;

import com.shj.em.aa.dto.RoleSearchDTO;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;
import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.shj.em.common.annotation.Log;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.PageVO;

import com.shj.em.aa.entity.Role;
import com.shj.em.aa.service.RoleService;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import com.shj.em.base.controller.BaseController;


/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-07-06
 */
@RestController
@Slf4j
@RequestMapping("/aa/role")
public class RoleController extends BaseController<Role>{


    @Autowired
    RoleService roleService;

    @RequestMapping("/list")
    public PageVO<Role> list(@RequestBody RoleSearchDTO searchDTO){
        Page<Role> page = roleService.queryRolePage(searchDTO);
        PageVO<Role> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(page.getRecords());
        return pageVO;
    }

    @GetMapping("/{id}")
    ResMsg<Role> getRoleById(@PathVariable("id") Long id){
        Role role = roleService.selectById(id);
        return ResMsg.ok(role);
    }

    @DeleteMapping("/{id}")
    public ResMsg remove(@PathVariable("id") Long id){
        roleService.deleteById(id);
        return ResMsg.ok();
    }

    @PostMapping
    public ResMsg saveRole(@RequestBody  Role role){
        roleService.insertOrUpdate(role);
        return ResMsg.ok();
    }

    /*

	@Log("创建角色")
	@ResponseBody
	@PostMapping("/add")
	public ResMsg add(Role role){
        try{
            roleService.insert(role);
            return ResMsg.ok(new Role());
        }catch(Exception e){
            log.error("创建角色失败", e);
            return ResMsg.error("创建角色失败");
        }

	}

    @Log("更新角色")
	@ResponseBody
	@PostMapping("/edit")
	public ResMsg edit(Role role){
        try{
            roleService.updateById(role);
            return ResMsg.ok();
        }catch(Exception e){
            log.error("更新角色失败", e);
            return ResMsg.error("更新角色失败");
        }

	}

    @Log("删除角色")
	@PostMapping( "/remove")
	@ResponseBody
	public ResMsg remove(@RequestParam("id") Long id){
        try{
            roleService.deleteById(id);
            return ResMsg.ok();
        }catch(Exception e){
            return ResMsg.error();
        }
	}

    @Log("批量删除角色")
	@PostMapping( "/batchRemove")
	@ResponseBody
	public ResMsg batchRemove(@RequestParam("ids[]") Long[] ids){
        roleService.deleteBatchIds(Arrays.asList(ids));
		return ResMsg.ok();
	}
*/
}

